'use strict';
/**
 * 单位表
 */
module.exports = app => {
  const { STRING, UUID, NOW, DATE, UUIDV4, INTEGER, FLOAT } = app.Sequelize;
  const ODepartments = app.model.define('ODepartments', {
    id: {
      type: UUID,
      primaryKey: true,
      allowNull: false,
      defaultValue: UUIDV4,
      comment: '部门id'
    },
    name: {
      type: STRING(255),
      field: 'name',
      comment: '部门名'
    },
    unitId: {
      type: STRING(36),
      field: 'unit_id',
      comment: '单位id'
    },
    remark: {
      type: STRING(36),
      field: 'remark',
      comment: '备注'
    },
    createDate: {
      type: DATE,
      defaultValue: NOW,
      field: 'create_date',
      comment: '创建时间'
    },
    updateDate: {
      type: DATE,
      defaultValue: NOW,
      field: 'update_date',
      comment: '更新时间'
    },
  }, {
    // 去除createAt，updateAt
    timestamps: false,
    // 实例对应的表名
    tableName: 'o_departments',
    // 将createdAt对应到数据库的created_at字段
    createdAt: 'create_date',
    // 将updatedAt对应到数据库的updated_at字段
    updatedAt: 'updated_date'
  });

  ODepartments.associate = function () {
    app.model.Organization.Departments.belongsTo(app.model.Organization.Units, {
      foreignKey: 'unitId',
      targetKey: 'id'
    })

    app.model.Organization.Departments.hasMany(app.model.Organization.Staff, {
      foreignKey: 'departmentId',
      targetKey: 'id'
    })
  }

  return ODepartments;
};  